package com.wyhello.tube4.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.wyhello.tube4.dao.WorkingRecordsDAO;
import com.wyhello.tube4.pojo.Working;

public class WorkingRecordsDAOImpl implements WorkingRecordsDAO{
	private SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	/**
	 * 运行记录的录入
	 * @param  working 要录入的信息
	 * @return 返回是否录入成功
	 */
	@Override
	public boolean saveWorking(Working working) {
		Session session = sessionFactory.getCurrentSession();
		int i = 1;
		try{
			session.save(working);
		}catch(Exception e){
			i=0;
		}
		if(i==1){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 对运行记录进行批量删除
	 * @param ids 要删除的id
	 * @return 返回删除的条数
	 */
	@Override
	public int deleteWorking(String ids) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "delete from working where id in "+ids;
		int i = session.createSQLQuery(sql).executeUpdate();
		return i;
	}
	/**
	 * 查询所有的运行记录
	 * @return 返回所有运行记录
	 */
	@Override
	public List<Working> queryWorking() {
		Session session  =  sessionFactory.getCurrentSession();
		String hql = "from Working";
		List<Working> list = session.createQuery(hql).list();
		return list;
	}
	/**
	 * 根据id查询运行记录
	 * @param id 要查询的id
	 * @return 为ID的运行记录
	 */
	@Override
	public Working getWorkingById(int id) {
		Session session  =  sessionFactory.getCurrentSession();
		Working working = (Working) session.get(Working.class, Integer.valueOf(id));
		return working;
	}
	/**
	 * 对运行记录进行修改
	 * @param working 修改后的信息
	 * @return 修改是否成功
	 */
	@Override
	public boolean updateWorking(Working working) {
		Session session  =  sessionFactory.getCurrentSession();
		try{
			session.update(working);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	/**
	 * 根据锅炉ID查询
	 * @param working 锅炉id
	 * @return 信息列表
	 */
	@Override
	public List<Working> queryWorkingByResideId(int resideId) {
		Session session  =  sessionFactory.getCurrentSession();
		String hql = "from Working where resideId = " + resideId +" order by id desc";
		List<Working> list = session.createQuery(hql).list();
		return list;
	}
	/**
	 * 根据锅炉ID查询有结束时间
	 * @param working 锅炉id
	 * @return 信息列表
	 */
	@Override
	public List<Working> queryWorkingEndByResideId(int resideId) {
		Session session  =  sessionFactory.getCurrentSession();
		String hql = "from Working where resideId = " + resideId +" AND endTime IS NOT null order by endTime desc";
		List<Working> list = session.createQuery(hql).list();
		return list;
	}
	/**
	 * 最近一次运行记录
	 * @param working
	 * @return 信息列表
	 */
	@Override
	public List<Working> queryWorkingForWarn() {
		Session session  =  sessionFactory.getCurrentSession();
		String hql = "from Working where endTime IS NOT null order by endTime desc";
		List<Working> list = session.createQuery(hql).list();
		return list;
	}
	/**
	 * 根据锅炉ID查询没有有结束时间
	 * @param working 锅炉id
	 * @return true 正在运行 false 停止运行
	 */
	@Override
	public boolean queryNoWorkingEndByResideId(int resideId) {
		Session session  =  sessionFactory.getCurrentSession();
		String hql = "from Working where resideId = " + resideId +" AND endTime IS null order by endTime desc";
		List<Working> list = session.createQuery(hql).list();
		if(list.size()!=0){
			return true;
		}else{
			return false;
		}
	}
}
